#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char* argv[])
{
    printf("process is running.....\n");
    pid_t id = fork();
    assert(id != -1);

    // child
    if(id == 0)
    {
        sleep(1);
        // ./myexec ls -a -l --color=auto --> "./myexec" "ls" "-a" "-l" "--color=auto"
        execvp(argv[1], &argv[1]);
        
        //execl("/usr/bin/ls", "ls", "-a", "-l", "--color=auto", NULL);
        //execlp("ls", "ls", "-a", "-l", "--color=auto", NULL);
        
        //char* const argv[] = {"ls", "-a", "-l", "--color=auto", NULL};
        //execv("/usr/bin/ls", argv);
        //execvp("ls", argv);
        
        //execl("./mybin", "mybin", NULL);
        //execl("./mybin", "./mybin", NULL);    //这种方式也可以
        
        //execl("./mycpp", "mycpp", NULL);
        //execl("./mybin", "mybin", NULL);
        
        //char* const envp[] = {"MYENV=1234", NULL};
        //execle("./mybin", "mybin", NULL, envp);
        
        //putenv((char*)"MYENV=11223344");    // 将指定的环境变量导入到系统中，即environ执向的环境变量表
        //extern char** environ;
        //execle("./mybin", "mybin", NULL, environ);
        exit(1);    // 进程替换失败
    }
    
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0)
    {
        printf("wait success, exit code:%d, signal number:%d\n", (status >> 8) & 0xFF, status & 0x7F);
    }
    return 0;

    // .c -> exe -> load -> process -> 运行 -> 执行我们现在所写的代码
    //printf("process is running.....\n");
    //execl("/usr/bin/lsabc", "ls", "-a", "-l", "--color=auto", NULL);
    // execl的第一个参数是要执行的程序, 第二个参数是如何执行, exec函数都必须以NULL结尾
    //printf("process running done...\n");

    //return 0;
}
